########
# Usage: Rscript cell_rel_sample_rosePolar.R HCC LB4
# 传入参数： cohort = 'HCC'
#            sample = 'HCC009'
#
########
library(ggplot2)
library(RColorBrewer)

library(optparse)

option_list <- list(
  make_option("--b", default = "", type = "character", help = "blood mean file"),
  make_option("--t", default = "", type = "character", help = "tissue mean file"),
  make_option("--s", default = "", type = "character", help = "sample"),
  make_option("--bo", default = "", type = "character", help = "blood output png file"),
  make_option("--to", default = "", type = "character", help = "tissue output png file")
)
opt <- parse_args(OptionParser(option_list = option_list))

Args = commandArgs()
sample = opt$s
print(sample)

# path = 'D:\\exo\\905\\submit 6.1\\cell_rel'
# setwd(path)
# cohort = 'HCC'
# sample = 'HCC009'

file.blood =  opt$b
file.tissue = opt$t
outfile.blood =  opt$bo
outfile.tissue =  opt$to
w=9
h=7

### blood cell
data.cells = read.csv(file.blood, sep=',', header = TRUE, row.names=1)
data.cells = round(data.cells, 3)*100
data = data.cells[sample, ]
data = as.data.frame(t(data))
colnames(data) = 'Abundance'
cellNames = row.names(data)
data$Label = paste(cellNames, '\n', data$Abundance, "%")
data$`Blood cell` = paste(cellNames, ": ", data$Abundance, "%", sep="")

pdf(outfile.blood,  width=w, height=h)
colors = c(brewer.pal(11,"RdYlBu"), brewer.pal(11,"PiYG"), '#003300')
p = ggplot(data, aes(x = Label, y = Abundance, fill = `Blood cell`)) + 
  scale_fill_manual(values=colors)+
  geom_bar(stat = "identity", alpha = 2, width = 1) + 
  coord_polar()+
  labs(x = "", y = "", title = sample)+           
  guides(fill = guide_legend(ncol = 1, bycol = TRUE, override.aes = list(size = 5)))+     
  theme(legend.position = "left", legend.key.height = unit(0.6,'cm'), legend.text = element_text(face = 'bold',color = 'black',size = 10)) +
  theme(plot.title = element_text(hjust = 0.5), axis.title = element_text(size = 10, face = "bold",color = 'black'))+
  theme(axis.text.y = element_blank()) +   
  theme(axis.text.x = element_text(size=9, face = "bold",color = 'black'))+   
  theme(axis.ticks = element_blank())+    
  theme(panel.border = element_blank())    
print(p)
dev.off()


### tissue cell
data.cells = read.csv(file.tissue, sep=',', header = TRUE, row.names=1)
data.cells = round(data.cells, 3)*100
data = data.cells[sample, ]
data = as.data.frame(t(data))
colnames(data) = 'Abundance'
cellNames = row.names(data)
data$Label = paste(cellNames, '\n', data$Abundance, "%")
data$`Tissue cell` = paste(cellNames, ": ", data$Abundance, "%", sep="")

pdf(outfile.tissue,  width=w, height=h)
colors = c(brewer.pal(11,"Spectral"), brewer.pal(5,"PRGn"))
p = ggplot(data, aes(x = Label, y = Abundance, fill = `Tissue cell`)) + 
  scale_fill_manual(values=colors)+
  geom_bar(stat = "identity", alpha = 2, width = 1) + 
  coord_polar()+
  labs(x = "", y = "", title = sample)+           
  guides(fill = guide_legend(ncol = 1, bycol = TRUE, override.aes = list(size = 5)))+     
  theme(legend.position = "left", legend.key.height = unit(0.6,'cm'), legend.text = element_text(face = 'bold',color = 'black',size = 10)) +
  theme(plot.title = element_text(hjust = 0.5), axis.title = element_text(size = 10, face = "bold",color = 'black'))+
  theme(axis.text.y = element_blank()) +   
  theme(axis.text.x = element_text(size=9, face = "bold",color = 'black'))+   
  theme(axis.ticks = element_blank())+    
  theme(panel.border = element_blank())    
print(p)
dev.off()
